import Vue from "vue";
import VueRouter from "vue-router";

//测试组件
import Test from "@/components/common/Test";

//登录组件
import Login from "@/views/login/Login";

//注册组件
import Register from "@/views/register/Register";

//在线咨询组件
import Consult from "@/views/consult/Consult";

//心理知识组件
import Knowledge from "@/views/knowledge/Knowledge";
import KnowledgeDetail from "@/views/knowledge/knowledgedetail/KnowledgeDetail.vue";
import PublishKnowledge from "@/views/knowledge/publish/PublishKnowledge";

//心理社区组件
import Community from "@/views/community/Community";
import QuestionDetail from "@/views/community/question/QuestionDetail";
import AckQuestion from "@/views/community/askquestion/AckQuestion";

//心理评测组件
import Evaluation from "@/views/evaluation/Evaluation";
import EvaluationDetail from "@/components/evaluation/EvaluationDetail";
import EvaQuestionCard from "@/components/evaluation/EvaQuestionCard";
import PublishEvalution from "@/views/evaluation/publish/PublishEvalution";

//个人中心组件
import PersonalCenter from "@/views/personalcenter/PersonalCenter";

//管理中心组件
import ManageCenter from "@/views/managecenter/ManageCenter";

//管理员的组件
import UserManage from "@/views/admin/user/UserManage";
import KnowledgeManage from "@/views/admin/knowledge/KnowledgeManage";
import ConsultManage from "@/views/admin/consult/ConsultManage";
import CommunityManage from "@/views/admin/community/CommunityManage";
import EvaluationManage from "@/views/admin/evaluation/EvaluationManage";

Vue.use(VueRouter);

const routes = [
  /** 测试组件 */
  {
    path: "/test",
    name: "Test",
    component: Test,
    meta: { requiresAuth: false }, // 添加requiresAuth元信息，表示是否需要注册
  },

  /** 登录 */
  {
    path: "/",
    name: "Login",
    component: Login,
    meta: { requiresAuth: false }, // 添加requiresAuth元信息，表示是否需要登录
  },
  /** 注册 */
  {
    path: "/register",
    name: "Register",
    component: Register,
    meta: { requiresAuth: false }, // 添加requiresAuth元信息，表示是否需要注册
  },

  /** 心理知识 */
  {
    path: "/knowledge",
    name: "Knowledge",
    component: Knowledge,
  },
  {
    path: "/knowledge/knowledgedtail/:id",
    name: "KnowledgeDetail",
    component: KnowledgeDetail,
  },
  {
    path: "/knowledge/publishknowledge",
    name: "PublishKnowledge",
    component: PublishKnowledge,
  },

  /** 在线咨询 */
  {
    path: "/consult",
    name: "Consult",
    component: Consult,
  },

  /** 心理社区 */
  {
    path: "/community",
    name: "Community",
    component: Community,
  },
  /** 心理社区-问题详情 */
  {
    path: "/community/questiondetail/:userId/:artcleId",
    name: "questiondetail",
    component: QuestionDetail,
  },
  /** 心理社区-我要提问*/
  {
    path: "/community/ackquestion",
    component: AckQuestion,
  },

  /** 心理评测 */
  {
    path: "/evaluation",
    name: "Evaluation",
    component: Evaluation,
  },
  {
    path: "/evaluation/publishevaluation",
    name: "PublishEvalution",
    component: PublishEvalution,
  },
  /**心理评测-测试详情 */
  {
    path: "/evaluation/evaluationdetail/:evaluateId",
    name: "evaluationdetail",
    component: EvaluationDetail,
  },
  /**心理评测-测试题目 */
  {
    path: "/evaluation/evaluationdetail/:evaluateId/evaquestions",
    name: "evaquestions",
    component: EvaQuestionCard,
  },

  /** 个人中心 */
  {
    path: "/personalcenter",
    name: "PersonalCenter",
    component: PersonalCenter,
  },

  /** 管理中心 */
  {
    path: "/managecenter",
    name: "ManageCenter",
    component: ManageCenter,
  },

  /**
   * 管理员功能
   */
  {
    path: "/admin/usermanage",
    name: "UserManage",
    component: UserManage,
  },
  {
    path: "/admin/knowledgemanage",
    name: "KnowledgeManage",
    component: KnowledgeManage,
  },
  {
    path: "/admin/consultmanage",
    name: "ConsultManage",
    component: ConsultManage,
  },
  {
    path: "/admin/communitymanage",
    name: "CommunityManage",
    component: CommunityManage,
  },
  {
    path: "/admin/evaluationmanage",
    name: "EvaluationManage",
    component: EvaluationManage,
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

// 导航守卫
router.beforeEach((to, from, next) => {
  // 检查是否需要登录
  if (to.meta.requiresAuth !== false) {
    // 判断 localStorage 中是否存在 token
    const token = localStorage.getItem("token");

    if (token) {
      // 存在 token，允许跳转
      next();
    } else {
      // 不存在 token，重定向到登录页面
      next("/");
    }
  } else {
    // 不需要登录的页面，直接跳转
    next();
  }
});

export default router;
